package com.okwanshiwu.interceptor;

import com.okwanshiwu.util.MD5;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ***********************************************************************************************
 * 版权所有 (C)2016, 北京顺圆科技
 * <p>
 * 文件名称：
 * 内容摘要:
 * 当前版本：
 * 作    者： 
 * 完成日期：
 * 修改记录：
 * 修改日期：
 * 版 本 号：
 * 修 改 人：
 * 修改内容：
 ***********************************************************************************************
 */
public class WebInterceptor implements AsyncHandlerInterceptor{

	private final Logger logger = Logger.getLogger(WebInterceptor.class);
    
    /**
     * 请求前拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        request.setCharacterEncoding("utf-8");
        logger.debug("web请求的路径："+request.getRequestURI());
	    String code = MD5.md5(request.getRequestURI().concat("xiaoming"));
		String sign = request.getParameter("sign");
		if(code.equals(sign)){
			return true;
		}
		response.getWriter().print("{json}");
	    return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
//        logger.debug("spring拦截器Interceptor的方法afterCompletion");
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        logger.debug("spring拦截器Interceptor的方法afterConcurrentHandlingStarted");
    }
    
}
